package com.hctforgreen.greenservice.machineManager.impl;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.machineManager.MachineBase;
import com.hctforgreen.greenservice.model.LocationEntity;
import com.hctforgreen.greenservice.model.PwdEntity;
import com.hctforgreen.greenservice.model.SYHotWaterMachineEntity;
import com.hctforgreen.greenservice.ui.adapter.SpinnerStringsAdapter;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.Utils;

import android.app.Activity;
import android.os.Handler;
import android.os.Parcel;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.Serializable;

/**
 * 商用热水机
 * @author 390694
 *
 */
public class SY_HotWaterMachine extends MachineBase implements Serializable {
	
	private View layout_SY_HotWaterMachine;
	private Spinner spinner_EngineeringType;
	private Spinner spinner_use_of_places;
	/**
	 * 商用热水机 使用场所
	 */
	private String operationSite;
	
	public ComHotWater comHotWater;
	public ComFloorHeat comFloorHeat;
	
	/**
	 * 工程类型: 1热水工程；2地热工程
	 */
	public int engineeringType = 0;
	/**
	 * 初始化
	 * @param context
	 */
	public SY_HotWaterMachine(Activity context) {
		super(context);
		comHotWater = new ComHotWater(context);
		comFloorHeat = new ComFloorHeat(context);

		//390697 屏蔽是为了在机型提示的关联资料中去获取开机密码
//		initView();
	}

	
	
	@Override
	public void initView() {
		layout_SY_HotWaterMachine = findViewById(R.id.Layout_SY_HotWaterMachine);
		spinner_EngineeringType = (Spinner) findViewById(R.id.spinner_EngineeringType);
		spinner_use_of_places = (Spinner) findViewById(R.id.spinner_use_of_places);
	}
	
	public void showView() {
		layout_SY_HotWaterMachine.setVisibility(View.VISIBLE);
		initEngineeringType();
		initHWUse_of_places();
		comHotWater.showView();
	}
	
	public void hideView(){
		layout_SY_HotWaterMachine.setVisibility(View.GONE);
		comHotWater.hideView();
		comFloorHeat.hideView();
	}
	
	
	@Override
	public boolean validateView() {
		boolean flag = true;
		if ( operationSite == null||engineeringType==0) {
			Utils.showToastCenter(activity,
					getString(R.string.barcode_info_is_incomplete));
			flag = false;
		}
		if(engineeringType==1){
			flag = comHotWater.validateView();
		}else{
			flag = comFloorHeat.validateView();
		}
		
		return flag;
	}


	@Override
	public HctResult getPwdResult(PwdEntity paramEntity,LocationEntity locationEntity, boolean isClounServer) throws Exception {
		HctResult result = new HctResult();
		if(engineeringType == 1){
			comHotWater.setOperationSite(operationSite);
			result = comHotWater.getPwdResult(paramEntity, locationEntity, isClounServer);
		}else if(engineeringType == 2){
			comFloorHeat.setOperationSite(operationSite);
			result = comFloorHeat.getPwdResult(paramEntity, locationEntity, isClounServer);
		}
		
		return result;
	}
	//关联提示页面用
	public HctResult getPwdResult(PwdEntity paramEntity,LocationEntity locationEntity, boolean isClounServer,SYHotWaterMachineEntity syHotWaterMachineEntity) throws Exception {
		HctResult result = new HctResult();
		if(syHotWaterMachineEntity.getEngineeringType() == 1){
			comHotWater.setOperationSite(syHotWaterMachineEntity.getOperationSite());
			result = comHotWater.getPwdResult(paramEntity, locationEntity, isClounServer,syHotWaterMachineEntity);
		}else if(syHotWaterMachineEntity.getEngineeringType() == 2){
			comFloorHeat.setOperationSite(syHotWaterMachineEntity.getOperationSite());
			result = comFloorHeat.getPwdResult(paramEntity, locationEntity, isClounServer,syHotWaterMachineEntity);
		}

		return result;
	}
	/**
	 * 商用热水机-：使用场所
	 */
	private void initHWUse_of_places() {

		final String[] items = getResources().getStringArray(
				R.array.use_of_places);
		operationSite = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_use_of_places.setAdapter(adapter);

		spinner_use_of_places.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					operationSite = null;
				} else {
					operationSite = items[arg2];
				}
				comHotWater.syHotWaterMachineEntity.setOperationSite(operationSite);
				comFloorHeat.syHotWaterMachineEntity.setOperationSite(operationSite);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}

	private void initEngineeringType(){
		
		spinner_EngineeringType.setPrompt(getString(R.string.tag_select_hint));
		final String[] items = getResources().getStringArray(
				R.array.form_of_engineering_application);
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_EngineeringType.setAdapter(adapter);

		spinner_EngineeringType.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				switch (arg2) {
				case 0:
					engineeringType = 0;
					break;
				case 1:
					comHotWater.showView();// 热水工程
					comFloorHeat.hideView();
					engineeringType = 1;
					comHotWater.syHotWaterMachineEntity.setEngineeringType(engineeringType);
					break;
				case 2:
					comFloorHeat.showView();// 地暖工程
					comHotWater.hideView();
					engineeringType = 2;
					comFloorHeat.syHotWaterMachineEntity.setEngineeringType(engineeringType);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}
}
